package cn.stylefeng.guns.modular.controller;


import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.entity.IdjActivityApply;
import cn.stylefeng.guns.modular.entity.responseVo.ActivityCountVo;
import cn.stylefeng.guns.modular.service.IIdjActivityApplyService;
import cn.stylefeng.guns.modular.util.JsonResult;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;

/**
 * <p>
 * 活动报名 前端控制器
 * </p>
 *
 * @author TANGWJ
 * @since 2020-08-04
 */
@Controller
@RequestMapping("/modular/activity_apply")
public class IdjActivityApplyController {

	@Autowired
	private IIdjActivityApplyService service;

	private static String PREFIX = "/activity-apply/";

	/**
	 * 跳转首页
	 */
	@RequestMapping
	public String index() {
		return PREFIX + "activity_apply.html";
	}
	
	@RequestMapping("/activity_count")
	public String show() {
		return PREFIX + "activity_count.html";
	}
	
	@RequestMapping("/refuse")
	public String refuse(Long id,String scoring, ModelMap map) {
		map.put("id", id);
		map.put("scoring", scoring);
		return PREFIX + "activity_apply_refuse.html";
	}
	
	 /**
     * 获取列表
     */
    @ResponseBody
    @RequestMapping("list")
    public LayuiPageInfo list(String userName, String division, Integer userType, String timeLimit, Integer status, Integer sex) {
        //获取分页参数
        Page<IdjActivityApply> page = LayuiPageFactory.defaultPage();
        //拼接查询条件
        String beginTime = null;
        String endTime = null;
        if (ToolUtil.isNotEmpty(timeLimit)) {
            String[] split = timeLimit.split(" - ");
            beginTime = split[0];
            endTime = split[1];
        }
        status = 1;
        String orderBy="orderBy";
        //根据条件查询日志
        return service.getList(page, userName, division, userType, beginTime, endTime, status, sex, orderBy, null);
    }
    
    /**
     * 获取列表
     */
    @ResponseBody
    @RequestMapping("lists")
    public LayuiPageInfo lists(String userName, String division, Integer userType, String timeLimit, Integer status, Integer sex) {
    	//获取分页参数
    	Page<IdjActivityApply> page = LayuiPageFactory.defaultPage();
    	//拼接查询条件
    	String beginTime = null;
    	String endTime = null;
    	if (ToolUtil.isNotEmpty(timeLimit)) {
    		String[] split = timeLimit.split(" - ");
    		beginTime = split[0];
    		endTime = split[1];
    	}
    	status = 2;
    	String order="order";
    	//根据条件查询日志
    	return service.getList(page, userName, division, userType, beginTime, endTime, status, sex, null, order);
    }
    
    @RequestMapping("details/{activityId}")
    public ModelAndView add(@PathVariable Long activityId) {
        ModelAndView index = service.edit(activityId);
        index.setViewName(PREFIX + "activity_apply_details.html");
        return index;
    }
    
	@RequestMapping("/update")
	@ResponseBody
	public ResponseData update(@RequestBody IdjActivityApply apply) {
		if (ToolUtil.isEmpty(apply.getId())) {
			throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
		}
		service.setStatus(apply);
		return ResponseData.success();
	}
	
	@RequestMapping("/detailed")
	@ResponseBody
	public JsonResult getProuctCount(){
		JsonResult rs = new JsonResult();
		List<ActivityCountVo> countVo = service.selectDivisionCount();
		rs.addContent("countVo", countVo);
		ActivityCountVo count = service.selectSumCount();
		rs.addContent("count", count);
		return rs.declareSuccess();
	}
}

